package m.jclecai.com.activity;


import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.adapter.BetSfc_RjcAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.Content;
import m.jclecai.com.dialog.BettingDialogFragment;
import m.jclecai.com.listener.ISelectPlayListener;
import m.jclecai.com.listener.NotifyTextViewSetChanged;
import m.jclecai.com.utils.CalculateBetNums;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.StringUtils;

import static m.jclecai.com.utils.CalculateBetNums.getBonus;

public class BettingSfc_RjcActivity extends BaseActivity implements View.OnClickListener,
        NotifyTextViewSetChanged ,ISelectPlayListener {

    @BindView(R.id.tv_mid_title)
    TextView tv_mid_title;
    @BindView(R.id.rl_bar)
    RelativeLayout rl_bar;
    @BindView(R.id.include_term_stop_time)
    TextView include_term_stop_time;
    @BindView(R.id.include_listview)
    ListView include_listview;
    @BindView(R.id.include_bet_way)
    TextView include_bet_way;
    @BindView(R.id.include_multiple_context)
    EditText include_multiple_context;
    @BindView(R.id.toolbar_confirm)
    TextView toolbar_confirm;
    @BindView(R.id.tv_bonus)
    TextView tv_bonus;

    BetSfc_RjcAdapter adapter;
    List<Content> mDatas = null;
    //注数
    int voteNums;
    //倍数
    String multiple;
    //是否有默认值要选中
    boolean check = true;
    //要选中的默认值过关方式
    private String pass="";
    //奖金优化
    String vote,name;
    int buyMoney = 0;
    String perfix = "";
    //当前选中的过关方式 默认0为第一个
    private int currentDialoItemCheck = 0;
    private int selectLength = 9;


    @Override
    protected int getContentView() {
        return R.layout.activity_betting_jczq;
    }

    @Override
    protected void initView() {
        super.initView();
        leftOptionText("");
        setTitle("确认投注");
        View inflate = View.inflate(this, R.layout.lottery_list_footer, null);
        inflate.findViewById(R.id.betting_item_footer_clear).setOnClickListener(this);
        include_listview.addFooterView(inflate);
    }

    @Override
    protected void initData() {
        super.initData();
        mDatas = JSON.parseArray(getIntent().getStringExtra("bean"), Content.class);
        perfix = getIntent().getStringExtra("perfix");
        selectLength = getIntent().getIntExtra("selectLength",9);
        adapter = new BetSfc_RjcAdapter(this, mDatas, this,perfix);
        include_listview.setAdapter(adapter);
        String[] names = getResources().getStringArray(R.array.lottery_pass_types_name_list);
        names = StringUtils.filterData(names,"串",mDatas.size());
        //默认选中的过关方式
        pass = names[names.length - 1];
        include_bet_way.setText(pass.replaceAll("1串1","单关"));
        getPredictBonus();
    }

    //过关方式
    @OnClick(R.id.include_bet_way)
    void showPassType() {
        if(mDatas.size() < selectLength){
            MyToast.showToast(this,"至少选择"+selectLength+"场比赛");
            return;
        }
        BettingDialogFragment dialogFragment = new BettingDialogFragment();
        dialogFragment.show(getSupportFragmentManager(), "Dialog");
        dialogFragment.setItemCount(mDatas.size());
        dialogFragment.setListener(this);
        dialogFragment.setDefCheck(check,pass,currentDialoItemCheck);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.betting_item_footer_clear:
                if (mDatas.size() > 0)
                    showAlertDialog("提示", "确认清空所有数据?", "取消", "确认", null, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mDatas.clear();
                            adapter.notifyDataSetChanged();
                        }
                    });
                break;
        }
    }

    @Override
    public void onClickSetChangedText() {
        tv_bonus.setText("");
        include_bet_way.setText("过关方式");
        this.pass = "";
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.tv_back)
    void back() {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("bean", JSON.toJSONString(mDatas));
        if (mDatas != null && mDatas.size() > 0)
            setResult(RESULT_OK, intent);
        else
            setResult(RESULT_OK);
        finish();
    }

    /**
     * 预测奖金
     */
    private void getPredictBonus() {
        if(mDatas.size() < selectLength){
            MyToast.showToast(this,"至少选择2场比赛");
            tv_bonus.setText("");
            include_bet_way.setText("过关方式");
            this.pass = "";
            return;
        }
        //算预测奖金
        try{
            vote = StringUtils.getBonusCode(mDatas,perfix.toUpperCase());
            Log.e("getBonusCode",getCodes());
            if(TextUtils.isEmpty(vote) || TextUtils.isEmpty(pass)){
                MyToast.showToast(getBaseContext(),"请选择过关方式");
                return;
            }
            String buyType = pass.replaceAll("串","*");
            CalculateBetNums calculateBetNums = new CalculateBetNums();
            //得到的是注数
            voteNums = calculateBetNums.getVoteNums(vote, buyType);
            List<String> rebuildVote = CalculateBetNums.rebuildVote(vote, buyType);
            String sp_max = getBonus(rebuildVote, 1, 1);//最大奖金
            String sp_min = getBonus(rebuildVote, 0, 1);//最小奖金
            multiple = include_multiple_context.getText().toString().trim();
            int num = Integer.parseInt(multiple);
            String money = (num * Float.parseFloat(sp_min)) + "~" + (num * Float.parseFloat(sp_max));
            buyMoney = (voteNums * num * 2);
            tv_bonus.setText(voteNums+"注 "+num+"倍 "+ buyMoney +"元 \n 奖金 "+money);

        }catch (Exception e){
            MyToast.showToast(getBaseContext(),"计算异常");
            e.printStackTrace();
        }
    }

    String getCodes(){
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<mDatas.size();i++){
            Content content = mDatas.get(i);
            if(content.isOdd_home_select())
                sb.append("3");
            if(content.isOdd_draw_select())
                sb.append("1");
            if(content.isOdd_away_select())
                sb.append("0");
            if(i<mDatas.size()-1)
                sb.append(",");
        }
        sb.append(":1:2");
        return sb.toString();
    }

    @Override
    public void select(String value, int itemPosition) {
        currentDialoItemCheck = itemPosition;
        this.pass = value;
        check = !TextUtils.isEmpty(value);
        if (TextUtils.isEmpty(value)) {
            include_bet_way.setText("过关方式");
            tv_bonus.setText("");
        } else{
            include_bet_way.setText(value.replaceAll("1串1", "单关")
                    .replaceAll("[*]", "串"));
        }
        getPredictBonus();
    }
}
